package com.yichang.comisiones.dao.impl;

import com.yichang.comisiones.dao.MesDao;
import com.yichang.comisiones.dto.MesDTO;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author josh
 */
@Repository
public class MesDaoImpl extends SimpleJdbcDaoSupport implements MesDao {

    private static Logger log = LoggerFactory.getLogger(MesDaoImpl.class);    
    
    @Autowired
    public MesDaoImpl(DataSource dataSource) {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }    
    
    @Override
    public List<MesDTO> listarMeses() {
        try {
            StringBuilder sql = new StringBuilder();
            sql.append("Set Language Spanish ");
            sql.append("select  ");
            sql.append("        distinct cod_mes codigoMes,  ");
            sql.append("        cod_mes/100 anho, ");
            sql.append("        cod_mes % 100 mes, ");
            sql.append("        DATENAME(month, STR(YEAR(GETDATE()), 4) + REPLACE(STR(cod_mes % 100, 2), ' ', '0') + '01') mesStr   ");
            //sql.append("from ltm_dia ");
            sql.append(" from ltm_dia where (cod_mes/100) > 2012 or ((cod_mes/100) = 2012 and cod_mes % 100>=6) ");
            sql.append("order by 1 ");
            return getSimpleJdbcTemplate().query(sql.toString(),new BeanPropertyRowMapper<MesDTO>(MesDTO.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}